require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function ($, common, ready, Collection, template, Model, dialog, datepicker) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            var _this = this;
            loading();
            this.event();
            //分类
            $.ajax({
                url: _global.url.api + 'categorys',
                type: 'get',
                async: false,
                data: {depth: 1},
                success: function (res) {
                    if (res.code == 0) {
                        _this.category = res.data;
                    }
                }
            });
            this.fetch();
        },
        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'new_audit',
                condition: {limit: 20, status: "untreated"},//查询条件
                model: {
                    url: _global.url.api + 'new_audit',
                    event: {
                        '.item-agree:click': 'agree',
                        '.item-stop:click': 'stop',
                        '.item-preview:click': 'preview'
                    },
                    agree: function (e) {

                        var model = this;
                        var data = model.data;

                        e.preventDefault();
                        dialog({
                            //title: '编辑活动',
                            content: '确定通过吗?',
                            width: 300,
                            quickClose: true,
                            ok: function () {
                                data.reviewed_at = new Date().getTime();
                                if (new Model({
                                        url: _global.url.api + 'new_audit/agree',
                                        data: data
                                    }).put()) {
                                }
                                _this.collection.fetch();
                            }, cancel: function () {
                            }
                        }).show();

                    },
                    stop: function (e) {
                        var model = this;
                        var data = model.data;

                        e.preventDefault();
                        dialog({
                            //title: '编辑活动',
                            content: '确定下架吗?',
                            width: 300,
                            quickClose: true,
                            ok: function () {
                                data.reviewed_at = new Date().getTime();
                                if (new Model({
                                        url: _global.url.api + 'new_audit/stop',
                                        data: data
                                    }).put()) {
                                }
                                _this.collection.fetch();
                            }, cancel: function () {
                            }
                        }).show();

                    },
                    preview: function (e) {
                        var model = this;
                        var data = model.data;
                        var id = data.good_id;
                        _this.privew_goods(id);
                    }
                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
            this.showCategory();
        },
        event: function () {
            //this.data = {};
            var _this = this;
            //查询
            $('#btn-search').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                _this.collection.condition.cate_id = $('select[name="cate"]').val()
                _this.collection.reload();
            });
            //根据状态
            $('#btn-status').find('a').on('click', function (e) {
                e.preventDefault();
                $('input[name="keyword"]').val('');
                _this.collection.condition.keyword = '';
                _this.collection.condition.status = $(e.target).attr('rel');
                if(_this.collection.condition.status=='stop'||_this.collection.condition.status=='treated'){
                    $("#all-ok").css({"display":"none"});
                    $("#all-stop").css({"display":"none"});
                }else if(_this.collection.condition.status=='untreated'){
                    $("#all-ok").css({"display":"inline-block"});
                    $("#all-stop").css({"display":"inline-block"});
                }
                _this.collection.reload();
                $(e.target).addClass('active').siblings().removeClass('active');
            });
            //根据分类
            $('#cate').on('change', function (e) {
                e.preventDefault();
                _this.collection.condition.cate_id = $("#cate").val();
                _this.collection.reload();
            });
            //批量审批
            $('#all-ok').on('click', function (e) {
                e.preventDefault();
                var good_id = [];
                $.each($('input[name="goods_id"]:checked'), function () {
                    good_id.push($(this).val());
                });
                if (good_id.length === 0) {
                    Alert({msg: '请选择商品', type: 'error'});
                    return;
                }
                var reviewed_at = new Date().getTime();
                dialog({
                    title: '批量处理',
                    content: template('all_tpl', {goods_ids: good_id}),
                    width: 200,
                    ok: function () {
                        new Model({
                            url: _global.url.api + 'new_audit/agree',
                            data: {
                                good_id: good_id,
                                reviewed_at: reviewed_at
                            }
                        }).put();
                        _this.collection.reload();

                    }, cancel: function () {
                    }
                }).show();
            });
            //批量下架
            $('#all-stop').on('click', function (e) {
                e.preventDefault();
                var good_id = [];
                $.each($('input[name="goods_id"]:checked'), function () {
                    good_id.push($(this).val());
                });
                if (good_id.length === 0) {
                    Alert({msg: '请选择商品', type: 'error'});
                    return;
                }
                var reviewed_at = new Date().getTime();
                dialog({
                    title: '批量处理',
                    content: template('all_tpl', {goods_ids: good_id}),
                    width: 200,
                    ok: function () {
                        new Model({
                            url: _global.url.api + 'new_audit/stop',
                            data: {
                                good_id: good_id,
                                reviewed_at: reviewed_at
                            }
                        }).put();
                        _this.collection.reload();

                    }, cancel: function () {
                    }
                }).show();
            });

        },

        privew_goods: function (goods_id) {
            this.data = {};
            var _this = this;
            var html = template('preview_goods-tpl', {goods_id: goods_id});
            _this.dialog = dialog({
                title: '预览商品',
                content: html,
                width: 320,
                height: 568,
                ok: function () {

                }, cancel: function () {
                }
            });
            _this.dialog.show();
        },
        showCategory: function () {
            var _this = this;
            //一级分类
            $.each(_this.category, function (n, v) {
                if (v.pid == null) {
                    $('[name="cate"]').append('<option value="' + v.id + '">' + v.name + '</option>');
                }
            });
        },

    };

    new index();
});